package toolbar

import (
	"yunj/pkg/yunj/core/builder/builderaction"
	"yunj/pkg/yunj/core/builder/builderconsts"
)

// 异步事件
type AsyncEvent struct {
	Action
}

func NewAsyncEvent(code any, title ...string) *AsyncEvent {
	a := &AsyncEvent{}

	builderaction.SetActionInstance(&a.Action.Action, a)
	a.SetActionCode(code)
	if len(title) > 0 {
		a.SetTitle(title[0])
	}
	return a
}

// 设置标题
func (a *AsyncEvent) SetTitle(title string) *AsyncEvent {
	a.Title = title
	return a
}

// 设置额外的class或图标类名
func (a *AsyncEvent) SetClass(class string) *AsyncEvent {
	a.Class = class
	return a
}

// 设置触发事件执行的url
func (a *AsyncEvent) SetUrl(url string) *AsyncEvent {
	a.Url = url
	return a
}

// 设置确认弹窗表单字段
// param: func() ([]form.FieldInterface, error)
// param: []form.FieldInterface
// param: form.FieldInterface
func (a *AsyncEvent) SetConfirmFormFields(param ...any) *AsyncEvent {
	builderaction.SetConfirmFormFields(&a.Action.Action, param...)
	return a
}

// 设置是否下拉操作
func (a *AsyncEvent) SetDropdown(yes bool) *AsyncEvent {
	a.Dropdown = yes
	return a
}

// 设置权限
func (a *AsyncEvent) SetAuth(auth string) *AsyncEvent {
	a.Auth = auth
	return a
}

// 处理属性
func (a *AsyncEvent) HandleAttr() error {
	// 设置默认值
	a.Type = builderconsts.EVENT_ASYNC_EVENT
	return nil
}
